package com.aiit.controller;

import com.aiit.pojo.Department;
import com.aiit.pojo.User;
import com.aiit.service.DepartmentService;
import com.aiit.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/04/30/18:11
 */
@Controller
public class DepartmentController {
    @Autowired
    private UserService userService;

    @Autowired
    private DepartmentService departmentService;
    @GetMapping("/department_table")
    public String department_table(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   Model model){
        List<Department> depart = departmentService.getAllByDepartments();
         model.addAttribute("depart",depart);
         //分页查询数据
        Page<Department> departmentPage= new Page<>(pn, 5);
        //分页查询结果
        Page<Department> page1 = departmentService.page(departmentPage, null);
        model.addAttribute("departpage",page1);

//        Page<User> page = new Page<>(pn, 5);
//        //数据库数据
//        List<User> records = page.getRecords();
//        model.addAttribute("pageuser",records);
//
        //当前页
        long current = page1.getCurrent();
        model.addAttribute("departcurrent",current);
        //分页总页数
        page1.getPages();
        //总记录数
        long total = page1.getTotal();
        return "/departmenttable/department_table";
    }
    @GetMapping("department/delete/{uid}")
    public String deleteDepartment(@PathVariable("uid") Integer uid,
                                    @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra){
        ra.addAttribute("pn",pn);
        departmentService.removeById(uid);
        return "redirect:/department_table";
    }
    @RequestMapping(value = "/departmentadd",method = RequestMethod.POST)
    public String departadd(Department department){
        departmentService.department(department);
        return "redirect:/department_table";
    }
}
